package com.wsj.message_server.utils;

import com.tencentyun.TLSSigAPIv2;

import java.util.Random;

public class IMUtils {

    private final static long sdkAppId = 1400495684;

    private final static String sdkAppKey = "b87882f64218fbd7424dbea258832c9535a0df05f5974455a23e465ee76816fe";

    private static final String adminAccount = "administrator";

    private static final String onlineCheckUrl = "https://console.tim.qq.com/v4/openim/querystate";

    private static final String groupForbidUrl = "https://console.tim.qq.com/v4/group_open_http_svc/forbid_send_msg";

    private static final String groupForbidListUrl = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_shutted_uin";

    private static final String sendGroupMsgUrl = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";

    private static final String sendGroupNotificationMsgUrl = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_system_notification";

    private static final String friendAddUrl = "https://console.tim.qq.com/v4/sns/friend_add";

    private static final String friendGetUrl = "https://console.tim.qq.com/v4/sns/friend_get";

    private static final String friendDelUrl = "https://console.tim.qq.com/v4/sns/friend_delete";

    private static final String friendCheckUrl = "https://console.tim.qq.com/v4/sns/friend_check";

    private static final String friendUpdateUrl = "https://console.tim.qq.com/v4/sns/friend_update";

    private static final String friendGetListUrl = "https://console.tim.qq.com/v4/sns/friend_get_list";

    private static final String pushAllUrl = "https://console.tim.qq.com/v4/all_member_push/im_push";

    private static final String createGroupUrl = "https://console.tim.qq.com/v4/group_open_http_svc/create_group";

    private static final String importMemberUrl = "https://console.tim.qq.com/v4/group_open_http_svc/import_group_member";

    private static final String getJoinedGroupUrl = "https://console.tim.qq.com/v4/group_open_http_svc/get_joined_group_list";

    private static final String getGroupInfoURL = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_info";

    private static final String destroyGroupURL = "https://console.tim.qq.com/v4/group_open_http_svc/destroy_group";

    private static final String blackListAdd = "https://console.tim.qq.com/v4/sns/black_list_add";

    private static final String blackListDelete="https://console.tim.qq.com/v4/sns/black_list_delete";

    private static final String friendDeleteAll="https://console.tim.qq.com/v4/sns/friend_delete_all";
    /**
     * 生成签名
     *
     * @param account 账号id
     * @return
     */
    public static String createUserSign(String account) {
        TLSSigAPIv2 api = new TLSSigAPIv2(sdkAppId, sdkAppKey);
        return api.genSig(account, 180 * 86400);
    }

    /**
     * 生成查询用户状态的url
     * @return
     */
    public static String createUserQueryStateUrl(){
        String sign = createUserSign(adminAccount);
        return onlineCheckUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 禁言/取消禁言
     * @return
     */
    public static String createGroupForbidUrl(){
        String sign = createUserSign(adminAccount);
        return groupForbidUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 禁言列表
     * @return
     */
    public static String createGroupForbidListUrl(){
        String sign = createUserSign(adminAccount);
        return groupForbidListUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 发送普通消息
     * @return
     */
    public static String createSendGroupMsgUrl(){
        String sign = createUserSign(adminAccount);
        return sendGroupMsgUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     *  发送系统通知消息
     * @return
     */
    public static String createGroupNotificationMsgUrl(){
        String sign = createUserSign(adminAccount);
        return sendGroupNotificationMsgUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 添加好友
     * @return
     */
    public static String createFriendAddUrlUrl(){
        String sign = createUserSign(adminAccount);
        return friendAddUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 获取好友
     * @return
     */
    public static String createFiendGetUrl(){
        String sign = createUserSign(adminAccount);
        return friendGetUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 删除好友
     * @return
     */
    public static String createFiendDelUrl(){
        String sign = createUserSign(adminAccount);
        return friendDelUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 删除所有好友
     * @return
     */
    public static String createFiendDelAllUrl(){
        String sign = createUserSign(adminAccount);
        return friendDeleteAll
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 检测好友
     * @return
     */
    public static String createFiendCheckUrl(){
        String sign = createUserSign(adminAccount);
        return friendCheckUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 好友更新
     * @return
     */
    public static String createFiendUpdateUrl(){
        String sign = createUserSign(adminAccount);
        return friendUpdateUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 添加黑名单
     * @return
     */
    public static String blackListAdd(){
        String sign = createUserSign(adminAccount);
        return blackListAdd
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 删除黑名单
     * @return
     */
    public static String blackListDelete(){
        String sign = createUserSign(adminAccount);
        return blackListDelete
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 获取好友资料
     * @return
     */
    public static String createFiendGetListUrl(){
        String sign = createUserSign(adminAccount);
        return friendGetListUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 全员推送
     * @return
     */
    public static String createPushAllUrl(){
        String sign = createUserSign(adminAccount);
        return pushAllUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }


    /**
     * 创建群
     * @return
     */
    public static String createGroupUrl(){
        String sign = createUserSign(adminAccount);
        return createGroupUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 导入群成员
     * @return
     */
    public static String importGroupMembersUrl(){
        String sign = createUserSign(adminAccount);
        return importMemberUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }

    /**
     * 获取用户所在的群组
     * @return
     */
    public static String getJoinedGroupList(){
        String sign = createUserSign(adminAccount);
        return getJoinedGroupUrl
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }


    /**
     * 获取群资料
     * @return
     */
    public static String getGroupInfoURL(){
        String sign = createUserSign(adminAccount);
        return getGroupInfoURL
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }




    public static String destroyGroupURL(){
        String sign = createUserSign(adminAccount);
        return destroyGroupURL
                + "?sdkappid=" + sdkAppId
                + "&identifier=" + adminAccount
                + "&usersig=" + sign
                + "&random=" + createRandom()
                + "&contenttype=json";
    }



    /**
     * 生成32位随机整数
     * @return
     */
    private static String createRandom(){
        Random rand = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= 10; i++) {
            int randNum = rand.nextInt(9) + 1;
            String num = randNum + "";
            sb = sb.append(num);
        }
        return String.valueOf(sb);
    }

}
